Makes a new stack that is a copy of source: the same items in the same order, with the same item on top, and the same capacity. Changing the copy never changes source, and the other way round. It goes on the right-hand side of the line that makes the variable, like stack_create, and it costs a whole second stack of memory.
stack_copy(source) -> stack<T>| Name | Type |
|---|---|
source | stack |
stack<T>
Makes a new stack that is a copy of source: the same items in the same order, so the same item is on top, and room for the same number of items. Changing the copy never changes source, and changing source never changes the copy.
var backup = stack_copy(source_stack);The copy has room for exactly as many items as source has room for - its capacity, which is not the same as how many it holds right now. A stack made with stack_create(64, 0) that holds 3 items makes a copy that holds the same 3 items and has room for 64. The capacity is the original's, and not the default of 8, so the copy is always big enough.
A copy takes its size from source, so there is no size for you to write. A copy given a smaller size of its own could not hold everything the original holds, so putting a size beside one (var backup: stack<int, 4> = stack_copy(source);, or a /// @capacity tag) is refused when the project is checked.
A copy is a second whole stack. The Game Boy Advance has no spare memory to hand out while a game runs, so the room for the copy is set aside when the game is built, in full, whether or not it ever fills up. A stack with room for 200 items makes a copy that takes room for 200 items, even if it only holds 3 right now. Two copies take the room of two. A copy in an object's variable keeps its room for as long as the object exists, even when nothing uses it any more.
Making the copy takes time as well. It walks through every item that source holds and copies each one, so the time grows with how many there are. A few items copied when something happens - a button press, a new level - is nothing. Hundreds of items copied twice in every frame is a real part of the frame spent copying, and it adds up on a real cartridge. Copy when you need to, not every frame.
Where the room comes from depends on where the variable lives:
backup = stack_copy(source);), the copy is a variable of the object, and every instance of the object has its own. An object that can have 30 instances at once has 30 copies.var (var backup = stack_copy(source);), the copy lives only while the event runs, on the console's stack - which is small. Keep a copy made this way small, and keep a big one in an object's variable instead.A copy is a new container, and its size belongs to the variable that holds it - so it goes in the same two places stack_create does, and nowhere else:
var: var backup = stack_copy(source);backup = stack_copy(source);Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists - that variable already has a size, and a copy of a different size cannot be put in it. To fill a stack from another one later in the game, empty it with stack_clear and push the items back one at a time in a loop.
source has to be a variable written on its own, like stack_copy(scores), whose size the compiler knows: one made in the same event, or an object variable made in Create. Anything else is refused with a plain message:
stack_copy(self.items)). Put it in a variable first.Giving it the wrong kind of container is refused too: stack_copy copies a stack and nothing else.
stack_length counts the items in the copy or in the original.
var history = stack_create(16, 0);
stack_push(history, 1);
stack_push(history, 2);
var saved = stack_copy(history); // 2 is on top of both stacks
var top = stack_pop(saved); // 2 - and history still holds both itemsA copy of the fixed-size vector a stack is stored in - the new one has the same N as source (its declared capacity, 8 if untagged). A stack that holds 3 items with room for 64 makes a copy that takes 64 places of memory. A copy is a second whole container, so it costs a second container's worth of memory: as many places as the original has room for (its capacity, not just how many it holds right now), and copying is O(n) in how many items the original holds. The Game Boy Advance has no heap, so the copy's size is fixed when the game is built - it is the original's own declared size, which is why the original has to be a variable whose size is known here (otherwise it is refused: a script's parameter could be any size). Like a creation call it has to be the whole right-hand side of the line that makes a variable, and for an object's variable that line is in its Create event, where every instance of the object gets its own copy of the memory. A copy is always the same size as the original, so a size written beside it is refused.